#include "operate_parm.h"
#include "main.h"

struct operating_paramter_summary operate_parm;
string my_str_tmp;
QString qstring_tmp;

void operate_data_update(void)
{

    float float_tmp = 0.0;
    if (1 == operate_parm.master_operate_parm.master_conctrller_state)
        master_state.master_state->set_text("正在自检");
    else if (2 == operate_parm.master_operate_parm.master_conctrller_state)
        master_state.master_state->set_text("完成自检");
    else if (3 == operate_parm.master_operate_parm.master_conctrller_state)
        master_state.master_state->set_text("自检失败");
    else if (4 == operate_parm.master_operate_parm.master_conctrller_state)
        master_state.master_state->set_text("自检告警");
    else
        master_state.master_state->set_red_text("无效");

    my_str_tmp = "";
    if (1 == operate_parm.master_operate_parm.master_err_code.bit.brake)
        master_state.master_brake->set_red_text("刹车");
    else
        master_state.master_brake->set_black_text("正常");

    if (1 == operate_parm.master_operate_parm.master_err_code.bit.commander_comm_err)
        master_state.commander_comm_err->set_red_text("异常");
    else
        master_state.commander_comm_err->set_black_text("正常");

    my_str_tmp = "";
    for (unsigned int i = 0; i < SLAVE_NUM; i++)
    {
        if (0x00 == operate_parm.laser_operate_parm[i].laser_op_state)
            slave_operate_parm[i].slave_state->set_black_text("自检成功");
        else if (0x01 == operate_parm.laser_operate_parm[i].laser_op_state)
            slave_operate_parm[i].slave_state->set_black_text("出光正常");
        else if (0x02 == operate_parm.laser_operate_parm[i].laser_op_state)
            slave_operate_parm[i].slave_state->set_black_text("出指示光");
        else if (0x03 == operate_parm.laser_operate_parm[i].laser_op_state)
            slave_operate_parm[i].slave_state->set_black_text("出1级弱光");
        else if (0x04 == operate_parm.laser_operate_parm[i].laser_op_state)
            slave_operate_parm[i].slave_state->set_black_text("故障状态");
        else if (0x05 == operate_parm.laser_operate_parm[i].laser_op_state)
            slave_operate_parm[i].slave_state->set_black_text("出2级弱光");
        else
            slave_operate_parm[i].slave_state->set_red_text("无效");

        float_tmp = operate_parm.laser_operate_parm[i].wave_length * 0.0001;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 4);
        qstring_tmp += "nm";
        slave_operate_parm[i].wave_length->set_text((char *)qstring_tmp.toStdString().c_str());

        float_tmp = operate_parm.laser_operate_parm[i].current1 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "A";
        slave_operate_parm[i].mo1_current->set_text((char *)qstring_tmp.toStdString().c_str());

        my_str_tmp = "";
        if (0 == operate_parm.laser_operate_parm[i].module1_state.all)
        {
            my_str_tmp += "正常";
            slave_operate_parm[i].mo1_state->set_black_text((char *)my_str_tmp.c_str());
        }
        else
        {
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.output_low_volt)
                my_str_tmp += "输出欠压 ";
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.brake)
                my_str_tmp += "外部刹车 ";
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.over_temperature_err)
                my_str_tmp += "超温故障 ";
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.output_low_current)
                my_str_tmp += "输出欠流 ";
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.output_over_current)
                my_str_tmp += "输出过流 ";
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.output_over_volt)
                my_str_tmp += "输出过压 ";
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.input_err)
                my_str_tmp += "输入异常 ";
            if (1 == operate_parm.laser_operate_parm[i].module1_state.bit.output)
                my_str_tmp += "正常输出 ";

            slave_operate_parm[i].mo1_state->set_red_text((char *)my_str_tmp.c_str());
        }

        float_tmp = operate_parm.laser_operate_parm[i].current2 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "A";
        slave_operate_parm[i].mo2_current->set_text((char *)qstring_tmp.toStdString().c_str());

        my_str_tmp = "";
        if (0 == operate_parm.laser_operate_parm[i].module2_state.all)
        {
            my_str_tmp += "正常";
            slave_operate_parm[i].mo2_state->set_black_text((char *)my_str_tmp.c_str());
        }
        else
        {
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.output_low_volt)
                my_str_tmp += "输出欠压 ";
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.brake)
                my_str_tmp += "外部刹车 ";
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.over_temperature_err)
                my_str_tmp += "超温故障 ";
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.output_low_current)
                my_str_tmp += "输出欠流 ";
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.output_over_current)
                my_str_tmp += "输出过流 ";
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.output_over_volt)
                my_str_tmp += "输出过压 ";
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.input_err)
                my_str_tmp += "输入异常 ";
            if (1 == operate_parm.laser_operate_parm[i].module2_state.bit.output)
                my_str_tmp += "正常输出 ";

            slave_operate_parm[i].mo2_state->set_red_text((char *)my_str_tmp.c_str());
        }

        float_tmp = operate_parm.laser_operate_parm[i].current3 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "A";
        slave_operate_parm[i].fa1_current->set_text((char *)qstring_tmp.toStdString().c_str());

        my_str_tmp = "";
        if (0 == operate_parm.laser_operate_parm[i].module3_state.all)
        {
            my_str_tmp += "正常";
            slave_operate_parm[i].fa1_state->set_black_text((char *)my_str_tmp.c_str());
        }
        else
        {
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.output_low_volt)
                my_str_tmp += "输出欠压 ";
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.brake)
                my_str_tmp += "外部刹车 ";
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.over_temperature_err)
                my_str_tmp += "超温故障 ";
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.output_low_current)
                my_str_tmp += "输出欠流 ";
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.output_over_current)
                my_str_tmp += "输出过流 ";
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.output_over_volt)
                my_str_tmp += "输出过压 ";
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.input_err)
                my_str_tmp += "输入异常 ";
            if (1 == operate_parm.laser_operate_parm[i].module3_state.bit.output)
                my_str_tmp += "正常输出 ";

            slave_operate_parm[i].fa1_state->set_red_text((char *)my_str_tmp.c_str());
        }

        float_tmp = operate_parm.laser_operate_parm[i].current4 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "A";
        slave_operate_parm[i].fa2_current->set_text((char *)qstring_tmp.toStdString().c_str());

        my_str_tmp = "";
        if (0 == operate_parm.laser_operate_parm[i].module4_state.all)
        {
            my_str_tmp += "正常";
            slave_operate_parm[i].fa2_state->set_black_text((char *)my_str_tmp.c_str());
        }
        else
        {
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.output_low_volt)
                my_str_tmp += "输出欠压 ";
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.brake)
                my_str_tmp += "外部刹车 ";
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.over_temperature_err)
                my_str_tmp += "超温故障 ";
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.output_low_current)
                my_str_tmp += "输出欠流 ";
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.output_over_current)
                my_str_tmp += "输出过流 ";
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.output_over_volt)
                my_str_tmp += "输出过压 ";
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.input_err)
                my_str_tmp += "输入异常 ";
            if (1 == operate_parm.laser_operate_parm[i].module4_state.bit.output)
                my_str_tmp += "正常输出 ";

            slave_operate_parm[i].fa2_state->set_red_text((char *)my_str_tmp.c_str());
        }

        my_str_tmp = to_string(operate_parm.laser_operate_parm[i].pd1 * 20);
        my_str_tmp += "mV";
        slave_operate_parm[i].pd1->set_text((char *)my_str_tmp.c_str());

        my_str_tmp = to_string(operate_parm.laser_operate_parm[i].pd2 * 20);
        my_str_tmp += "mV";
        slave_operate_parm[i].pd2->set_text((char *)my_str_tmp.c_str());

        my_str_tmp = to_string(operate_parm.laser_operate_parm[i].pd3 * 20);
        my_str_tmp += "mV";
        slave_operate_parm[i].pd3->set_text((char *)my_str_tmp.c_str());

        my_str_tmp = to_string(operate_parm.laser_operate_parm[i].pd4 * 20);
        my_str_tmp += "mV";
        slave_operate_parm[i].pd4->set_text((char *)my_str_tmp.c_str());

        float_tmp = operate_parm.laser_operate_parm[i].t1 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "℃";
        slave_operate_parm[i].t1->set_text((char *)qstring_tmp.toStdString().c_str());

        float_tmp = operate_parm.laser_operate_parm[i].t2 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "℃";
        slave_operate_parm[i].t2->set_text((char *)qstring_tmp.toStdString().c_str());

        float_tmp = operate_parm.laser_operate_parm[i].t3 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "℃";
        slave_operate_parm[i].t3->set_text((char *)qstring_tmp.toStdString().c_str());

        float_tmp = operate_parm.laser_operate_parm[i].t4 * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "℃";
        slave_operate_parm[i].t4->set_text((char *)qstring_tmp.toStdString().c_str());

        my_str_tmp = to_string(operate_parm.laser_operate_parm[i].humidity);
        my_str_tmp += "%";
        slave_operate_parm[i].humidity->set_text((char *)my_str_tmp.c_str());

        my_str_tmp = to_string(operate_parm.laser_operate_parm[i].intensity);
        my_str_tmp += "%";
        slave_operate_parm[i].intensity->set_text((char *)my_str_tmp.c_str());

        float_tmp = operate_parm.laser_operate_parm[i].temperature * 0.1;
        qstring_tmp.clear();
        qstring_tmp.setNum(float_tmp, 'f', 1);
        qstring_tmp += "℃";
        slave_operate_parm[i].temperature->set_text((char *)qstring_tmp.toStdString().c_str());

        my_str_tmp = "";
        if (1 == operate_parm.laser_operate_parm[i].running_state.bit.debug_mode)
            my_str_tmp += "调试模式 ";
        else
            my_str_tmp += "非调试模式 ";

        if (1 == operate_parm.laser_operate_parm[i].running_state.bit.standby_mode)
            my_str_tmp += "热待机 ";
        else
            my_str_tmp += "冷待机 ";

        if (1 == operate_parm.laser_operate_parm[i].running_state.bit.withdraw)
            my_str_tmp += "撤收 ";
        else
            my_str_tmp += "未撤收 ";

        slave_operate_parm[i].running_state->set_black_text((char *)my_str_tmp.c_str());

        if (0 == operate_parm.laser_operate_parm[i].seed_source_state)
            slave_operate_parm[i].seed_source_state->set_black_text("关闭");
        else if (1 == operate_parm.laser_operate_parm[i].seed_source_state)
            slave_operate_parm[i].seed_source_state->set_black_text("开启");
        else if (2 == operate_parm.laser_operate_parm[i].seed_source_state)
            slave_operate_parm[i].seed_source_state->set_red_text("通信异常");
        else
            slave_operate_parm[i].seed_source_state->set_red_text("无效");

        if (0 == operate_parm.laser_operate_parm[i].expansion_module_state)
            slave_operate_parm[i].expansion_module_state->set_black_text("关闭");
        else if (1 == operate_parm.laser_operate_parm[i].expansion_module_state)
            slave_operate_parm[i].expansion_module_state->set_black_text("开启");
        else if (2 == operate_parm.laser_operate_parm[i].expansion_module_state)
            slave_operate_parm[i].expansion_module_state->set_black_text("通信异常");
        else if (3 == operate_parm.laser_operate_parm[i].expansion_module_state)
            slave_operate_parm[i].expansion_module_state->set_black_text("功率异常");
        else
            slave_operate_parm[i].expansion_module_state->set_red_text("无效");

        my_str_tmp = "";
        if (0 == operate_parm.laser_operate_parm[i].fault_code1.all)
        {
            my_str_tmp = "正常";
            slave_operate_parm[i].err_code1->set_black_text((char *)my_str_tmp.c_str());
        }
        else
        {
            slave_operate_parm[i].err_code1->set_text_red();
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.pd1_err)
            {
                my_str_tmp += "PD1异常 ";
            }
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.pd2_err)
            {
                my_str_tmp += "PD2异常 ";
            }
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.pd3_err)
            {
                my_str_tmp += "PD3异常 ";
            }
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.pd4_err)
            {
                my_str_tmp += "PD4异常 ";
            }
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.t1_err)
            {
                my_str_tmp += "t1异常 ";
            }
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.t2_err)
            {
                my_str_tmp += "t2异常 ";
            }
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.t3_err)
            {
                slave_operate_parm[i].err_code1->set_text_red();
                my_str_tmp += "t3异常 ";
            }
            if (1 == operate_parm.laser_operate_parm[i].fault_code1.bit.t4_err)
            {
                slave_operate_parm[i].err_code1->set_text_red();
                my_str_tmp += "t4异常 ";
            }
            slave_operate_parm[i].err_code1->set_red_text((char *)my_str_tmp.c_str());
        }

        my_str_tmp = "";
        if (0 == operate_parm.laser_operate_parm[i].fault_code2.all)
        {
            my_str_tmp += "正常";
            slave_operate_parm[i].err_code2->set_black_text((char *)my_str_tmp.c_str());
        }
        else
        {
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.brake)
                my_str_tmp += "刹车 ";
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.seed_source_err)
                my_str_tmp += "种子源异常 ";
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.humidity_err)
                my_str_tmp += "湿度异常 ";
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.envi_err)
                my_str_tmp += "环境温度异常 ";
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.seq_err)
                my_str_tmp += "输出时序异常 ";
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.laser_comm_err)
                my_str_tmp += "光源通信异常 ";
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.power_comm_err)
                my_str_tmp += "驱动源通信异常 ";
            if (1 == operate_parm.laser_operate_parm[i].fault_code2.bit.master_comm_err)
                my_str_tmp += "集中控制器通信异常 ";

            slave_operate_parm[i].err_code2->set_red_text((char *)my_str_tmp.c_str());
        }
        
        my_str_tmp = "";
        if (0 == operate_parm.laser_operate_parm[i].fault_code3.all)
        {
            my_str_tmp += "正常";
            slave_operate_parm[i].err_code3->set_black_text((char *)my_str_tmp.c_str());
        }
        else
        {
            if (1 == operate_parm.laser_operate_parm[i].fault_code3.bit.expansion_module_err)
                my_str_tmp += "展宽模块异常 ";
                
            slave_operate_parm[i].err_code3->set_red_text((char *)my_str_tmp.c_str());
        }
        
        
        
        my_str_tmp = to_string(operate_parm.laser_operate_parm[i].expansion_module_id + 1);
        slave_operate_parm[i].expansion_module_id->set_text((char *)my_str_tmp.c_str());
    }
}
